//
//  LYTabbarController.m
//  LYTabbarDemo
//
//  Created by 刘宇 on 16/6/23.
//  Copyright © 2016年 liuyu. All rights reserved.
//

#import "LYTabbarController.h"


@interface LYTabbarController ()<LYBarDelegate,LYTabbarControllerDelegate>

@end

@implementation LYTabbarController


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.tabBar.hidden = YES;
        
        [self.customTabbar setBackgroundColor:[UIColor grayColor]];
        self.customTabbar = [[LYBar alloc]init];
        self.customTabbar.delegate = self;
        self.customTabbar.delegateCustom = self;
        [self setValue:self.customTabbar forKey:@"tabBar"];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)addChildViewControllers:(NSArray<UIViewController   *>*)childVCs images:(NSArray<NSString  *>*)images selectedImages:(NSArray<NSString  *>*)selectedImages {
    
    if (!childVCs) {
        if (_tabbarDelegate && [_tabbarDelegate respondsToSelector:@selector(tabBarController:error:)]) {
            [_tabbarDelegate tabBarController:self error:LYErrorControllersNULL];
        }
        return;
    }else if (!images){
        if (_tabbarDelegate && [self.delegate respondsToSelector:@selector(tabBarController:error:)]) {
            [_tabbarDelegate tabBarController:self error:LYErrorImagesNULL];
        }
        return;
    }else if (!selectedImages){
        if (_tabbarDelegate && [_tabbarDelegate respondsToSelector:@selector(tabBarController:error:)]) {
            [_tabbarDelegate tabBarController:self error:LYErrorSelectedImagesNULL];
        }
        return;
    }else{
        if ((childVCs.count == images.count) && (childVCs.count == selectedImages.count)) {
            
            self.childVCs = childVCs;
            self.images = images;
            self.selectedImages = selectedImages;
            
            if (_tabbarDelegate && [_tabbarDelegate respondsToSelector:@selector(tabBarController:error:)]) {
                [_tabbarDelegate tabBarController:self error:LYErrorNone];
            }
        }else{
            if (_tabbarDelegate && [_tabbarDelegate respondsToSelector:@selector(tabBarController:error:)]) {
                [_tabbarDelegate tabBarController:self error:LYErrorControllersAndImagesAndSelectedImagesCountDiff];
            }
            return;
        }
    }
    
    for (int i = 0; i < childVCs.count; i++) {
        
        // 设置子控制器的tabBarItem图片
        childVCs[i].tabBarItem.image = [UIImage imageNamed:images[i]];
        // 禁用图片渲染
        childVCs[i].tabBarItem.selectedImage = [[UIImage imageNamed:selectedImages[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        // 设置文字的样式
        [childVCs[i].tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : RGBColor(123, 123, 123)} forState:UIControlStateNormal];
        [childVCs[i].tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];

        LYNavigationController  *nav = [[LYNavigationController alloc]initWithRootViewController:childVCs[i]];
        
        [self addChildViewController:nav];
    }
    
    self.customTabbar.tabbarButtonCount = childVCs.count;
    
}
- (void)setCenterButtonStyle:(LYCenterButtonStyle)centerButtonStyle
{
    _centerButtonStyle = centerButtonStyle;
    if (centerButtonStyle == LYCenterButtonStyleDefault || centerButtonStyle == LYCenterButtonStyleNOBulge) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"CHANGE_BAR_BG" object:nil userInfo:@{@"tag":@"0"}];
        return;
    }
    [[NSNotificationCenter defaultCenter]postNotificationName:@"CHANGE_BAR_BG" object:nil userInfo:@{@"tag":@"1"}];
}
- (void)setCenterImage:(UIImage *)centerImage
{
    [self.customTabbar.centerButton setBackgroundImage:centerImage forState:UIControlStateNormal];
}
- (void)setCenterSelectedImage:(UIImage *)centerSelectedImage
{
    [self.customTabbar.centerButton setBackgroundImage:centerSelectedImage forState:UIControlStateHighlighted];
}
- (void)setCenterBounds:(CGRect )centerBounds
{
    self.customTabbar.centerButton.bounds = centerBounds;
}
- (void)setCenterButtonOffset:(CGFloat)centerButtonOffset
{
    _centerButtonOffset = centerButtonOffset;
    self.customTabbar.centerButtonOffset = _centerButtonOffset;
}

- (void)tabBar:(LYBar *)tabBar DidClickCenterButton:(UIButton *)centerButton
{
    //点击中间代码出发的动作请写在下面
    NSLog(@"点击了中间按钮");
}
@end
